// pages/communicate_word/communicate_word.js
const app = getApp(), core = require('../../utils/core');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sentence_id:0,
    sentence:[],
    isAddTextPop: false,
    currentText: "",
    maxTextLength: 200,
    isEditTextPop:false
  },
  // 关闭添加常用语弹窗
  toggleAddTextPop: function () {
    this.setData({
      isAddTextPop: !this.data.isAddTextPop
    })
  },
    // 关闭修改常用语弹窗
    toggleEditTextPop: function (e) {
      var isEditTextPop = this.data.isEditTextPop;
      if(!isEditTextPop){
        let index = e.currentTarget.dataset.index;
        let sentence = this.data.sentence;
        if(sentence[index].user_id == 0){
          wx.showToast({
            title: '内置信息不可编辑',
            icon:'none'
          })
          return false;
        }
        this.setData({
          sentence_id: sentence[index].id,
          isEditTextPop: !isEditTextPop,
          currentText:sentence[index].name
        })
      }else{
        this.setData({
          sentence_id: 0,
          isEditTextPop: !isEditTextPop,
          currentText:''
        })
      }
      
    },
  // 添加完成常用语
  addText: function () {
    var that = this;
    var currentText = that.data.currentText.trim();
    var sentence_id = that.data.sentence_id;
    core.post({
      url: '/xilujob.sentence/add_sentence', 
      data: {sentence_id:sentence_id,content:currentText},
      success: ret => {
        that.getSentence();
        that.setData({
          sentence_id: 0,
          isEditTextPop:false,
          isAddTextPop: false,
          currentText: ""
        })
      },fail: ret => {
        //wx.navigateBack();
        wx.showToast({title: ret.msg,icon:'none'});
        return false;
      }
    });
    
  },
   // 输入常用语
   textInput(e) {
    if (e.detail.value.length <= this.data.maxTextLength) {
      this.setData({
        currentText: e.detail.value,
      });
    } else {
      this.setData({
        currentText: this.data.currentText,
      })
      wx.showToast({
        title: '字数不超过' + this.data.maxTextLength + '字',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSentence();
  },
  //常用语
  getSentence() {
    core.get({url: '/xilujob.sentence', data: {},success: ret => {
        this.setData({
          sentence: ret.data
        })
      },fail: ret => {
        wx.showToast({title: ret.msg,icon:'none'});
        return false;
      }
    });
  },
  toggleDelText(e){
	var that = this;
	var sentence_id = e.currentTarget.dataset.id;
	wx.showModal({
	  title: '提示',
	  content: '确定删除？',
	  complete: (res) => {
		if (res.confirm) {
			core.post({url: '/xilujob.sentence/del_sentence', data: {sentence_id:sentence_id},success: ret => {
				  that.getSentence();
				}
			});
		}
	  }
	})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
